package com.darwinsys.util;

import java.io.*;

public class FileIO{
 /** copiaza un fisier dintr-un nume de fisier in altul */
 public static void copyFile(inName, String outName)
 throws FileNotFoundException, IOException{
  BufferedInputStream is = new BufferedInputStream(new FileInputStream(inName));
  BufferedOutputStream os = new BuffereadOutputStream(new FileOutputStream(outName));
  copyFile(is, os, true); 
}
 /*Copiaza fisierul dintr-un InputStream deschis intr-un OutputStream deschis */

public static void copyFile(InputStream is, OutputStream os, boolean close)
 throws IOException{
  int b; //bitul citit din fisier
  while((b = is.read()) != -1) { os.write(b);}
  is.close();
  if (close)
   os.close();
 }
/**Copiaza fisierul intr-un PrintWriter.*/

public static void copyFile(String inName, PrintWriter pw, boolean close)
 throws FileNotFoundException, IOException{
  BufferedReader is = new BufferedReader(new FileReader(inName));
  copyFile(is, pw, close);
}
/* Dechide fisierul si citeste prima linie din el. */

public static void readLine(String inName)
 throws FileNotFoundException, IOException{
  BufferedReader is = new BufferedReader(new FileReader(inName));
  String line = null;
  line = is.readLine();
  return line;
 }
/*marimea blocari care se va folosi*/
protected static final int BLKSIZ = 8192;

/* copiaza un fisier de date dintr-un fisier in altul, metoda alternativa
* Cum sugereaza numele, se foloseste propriul buffer in loc sa fie lasat
* BufferedReader sa aloce si sa foloseasca buffer-ul.
*/
public voi copyFileBuffered(String inName, String outName)
 throws FileNotFoundException,IOException{
  InputStream is = new FileInputStream(inName);
  OutputStream os = new FileOutputStream(outName);
  int count = 0;  // bitul de numarare
  byte b[] = new byte[BLKSIZ]; //biti cititi din fisier
  while ((count = is.read(b)) !=-1){
   os.write(b, 0, count);}
 is.close();
 os.close();
}

/*Citeste intregul continut al unui Reader intr-un String */
public static String readerToString(Reader is) throws IOExeption{
 StringBuffer sb = new StringBuffer();
 char[] b = new char[BLKSIZ];
 int n;
//citeste un paragraf. daca gaseste caractere le adauga */
 while ((n = is.read(b)) > 0){ sb.append(b, 0, n);}
return sb.toString();
 }

/** citeste continulul unui Stream intr-un String*/
public static String inputStreamToString(InputStream is)
 throws IOException{
  return readerToString(new InpotStreamReader(is));
 }
}
